package a02;

import java.util.LinkedList;
import java.util.List;

public class Position {
    private byte pos;
    private byte triblet;

    public Position(byte triblet, byte pos) {
	this.setTriblet(triblet);
	this.setPos(pos);
    }

    @Override
    public String toString() {
	return "(" + this.getTriblet() + "," + this.getPos() + ")";
    }

    public List<Position> connectedPositions() {
	final List<Position> result = new LinkedList<Position>();
	if (this.getPos() % 2 == 1) {
	    if (this.getTriblet() == 0 || this.getTriblet() == 2) {
		result.add(new Position((byte) 1, this.getPos()));
	    } else {
		result.add(new Position((byte) 0, this.getPos()));
		result.add(new Position((byte) 2, this.getPos()));
	    }
	}
	result.add(new Position(this.getTriblet(), (byte) ((this.getPos() + 7) % 8)));
	result.add(new Position(this.getTriblet(), (byte) ((this.getPos() + 1) % 8)));

	return result;
    }

    public List<Position[]> rows() {
	List<Position[]> result = new LinkedList<Position[]>();
	if (this.getPos() % 2 == 1) {
	    final Position[] r1 = { new Position((byte) 0, this.getPos()),
		    new Position((byte) 1, this.getPos()),
		    new Position((byte) 2, this.getPos()) };

	    final Position[] r2 = {
		    new Position(this.getTriblet(), (byte) ((this.getPos() + 7) % 8)),
		    new Position(this.getTriblet(), this.getPos()),
		    new Position(this.getTriblet(), (byte) ((this.getPos() + 1) % 8)) };
	    result.add(r1);
	    result.add(r2);
	} else {
	    final Position[] r1 = {
		    new Position(this.getTriblet(), (byte) ((this.getPos() + 1) % 8)),
		    new Position(this.getTriblet(), this.getPos()),
		    new Position(this.getTriblet(), (byte) ((this.getPos() + 2) % 8)) };

	    final Position[] r2 = {
		    new Position(this.getTriblet(), (byte) ((this.getPos() + 7) % 8)),
		    new Position(this.getTriblet(), this.getPos()),
		    new Position(this.getTriblet(), (byte) ((this.getPos() + 6) % 8)) };
	    result.add(r1);
	    result.add(r2);
	}

	return result;
    }

    public byte getTriblet() {
	return triblet;
    }

    public void setTriblet(byte triblet) {
	this.triblet = triblet;
    }

    public byte getPos() {
	return pos;
    }

    public void setPos(byte pos) {
	this.pos = pos;
    }
}
